package middleware

import (
	"github.com/micro/go-micro/v2"
	"rpc-service/product/proto/product"
	"rpc-service/user/proto/user"
	"web-service/admin-api/common/wappers"
)

/**
 * 统一管理RPC Client,在服务启动时创建一次
 */
type GrpcClient struct {
	ProductRpcClient product.ProdService
	UserRpcClient    user.UserService

	//其它的RPC调用写在这里Client...
	//OtherService other.OtherService
}

//全局变量管理RPC Service Client
var rpcClient GrpcClient

/**
 * 把需要远程调用的client都创建在这里
 */
func RegisterRpcClient() {

	//创建一个rpc调用client
	apiClient := micro.NewService(
		micro.Name("api.client"),
		//添加一个wrapper,对Call方法重写,前置一些统一的操作逻辑
		micro.WrapClient(wappers.NewClientWrapper),
	)

	//"product-service" 对应服务注册名和注册中心product-service服务名称保持一致
	rpcClient.ProductRpcClient = product.NewProdService("go.micro.svr.grpc.product", apiClient.Client())

	rpcClient.UserRpcClient = user.NewUserService("go.micro.svr.grpc.user", apiClient.Client())

	//以下可以注册更多其它service ...
	//serviceClient.OtherService =  other.NewOtherService("other-service", prodServiceClient.Client())
}

func GetProductRpcClient() product.ProdService {
	return rpcClient.ProductRpcClient
}


func GetUserRpcClient() user.UserService {
	return rpcClient.UserRpcClient
}
//以下可以写更多获取client方法
